using System;
using System.Net; 
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
using System.Net.Configuration;
using System.Configuration;
using System.Web.Configuration;

namespace SDT.Libraries.Utilities
{
    /// <summary>
    /// Summary description for MailClass
    /// </summary>
    public class MailProvider
    {
        #region Fields & Properties
        private string errorMessage = string.Empty;
        private string emailDisplayName = string.Empty;
        private bool useSSL = false;
        private MailSettingsSectionGroup settings;

        public bool UseSSL
        {
            set { useSSL = value; }
            get { return useSSL; }
        } 

        public string EmailDisplayName
        {
            set { emailDisplayName = value; }
            get { return emailDisplayName; }
        } 

        /// <summary>
        /// Gets or sets the error message.
        /// </summary>
        /// <value>The error message.</value>
        public string ErrorMessage
        {
            set { errorMessage = value; }
            get { return errorMessage; }
        } 
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="MailProvider"/> class.
        /// </summary>
        public MailProvider()
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
            settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
        }

        /// <summary>
        /// Sends the mail.
        /// </summary>
        /// <param name="emailTo">The email to.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <returns></returns>
        public bool SendMail(string emailTo, string subject, string body)
        {
            return SendMail(emailTo, "", "", subject, body);
        }

        /// <summary>
        /// Sends an mail message
        /// </summary>
        /// <param name="from">Sender address</param>
        /// <param name="to">Recepient address</param>
        /// <param name="bcc">Bcc recepient</param>
        /// <param name="cc">Cc recepient</param>
        /// <param name="subject">Subject of mail message</param>
        /// <param name="body">Body of mail message</param>
        public bool SendMail(string to, string bcc, string cc, string subject, string body)
        {
            try
            {
                // Instantiate a new instance of MailMessage
                using (MailMessage mailMessage = new MailMessage { })
                {
                    mailMessage.From = new MailAddress(settings.Smtp.From, EmailDisplayName);
                    // Set the recepient address of the mail message
                    mailMessage.To.Add(to);
                    // Check if the bcc value is null or an empty string
                    if (!string.IsNullOrEmpty(bcc))
                        // Set the Bcc address of the mail message
                        mailMessage.Bcc.Add(new MailAddress(bcc));
                    // Check if the cc value is null or an empty value
                    if (!string.IsNullOrEmpty(cc))
                        // Set the CC address of the mail message
                        mailMessage.CC.Add(new MailAddress(cc));
                    // Set the subject of the mail message
                    mailMessage.Subject = subject;
                    // Set the body of the mail message
                    mailMessage.Body = body;
                    // Set the format of the mail message body as HTML
                    mailMessage.IsBodyHtml = true;
                    // Set the priority of the mail message to normal
                    mailMessage.Priority = MailPriority.Normal;

                    // Instantiate a new instance of SmtpClient
                    SmtpClient smtpClient = new SmtpClient(settings.Smtp.Network.Host, settings.Smtp.Network.Port);
                    smtpClient.EnableSsl = UseSSL;

                    // Send the mail message
                    smtpClient.Send(mailMessage);
                }

                return true;
            }
            catch (Exception ex)
            {
                errorMessage = ex.ToString();
                return false;
            }
        }


        /// <summary>
        /// Determines whether the specified input email is email.
        /// </summary>
        /// <param name="inputEmail">The input email.</param>
        /// <returns>
        /// 	<c>true</c> if the specified input email is email; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsEmail(string inputEmail)
        {
            if (inputEmail == null | inputEmail.Length > 50) return false;
            Regex re = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            return (re.IsMatch(inputEmail));
        }
    }
}